<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class categoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
  {
    $this->middleware('login');
  }
    public function index(Request $request)
    {

        $num = $request -> input('num',20);
        $sort = $request ->input('sort');
        $keywords = $request -> input('keywords','');
        $data = DB::table('types as t1')
        ->select('t1.*','t2.tname as pname',DB::raw("concat(t1.path,',',t1.tid) as sortpath"))
        ->leftJoin('types as t2','t1.pid','=','t2.tid')
        ->where('t1.tname','like','%'.$keywords.'%')
        ->orderBy('sortpath')
        ->paginate($num);
        foreach ($data as $k => $v)
        {
           $num = substr_count($v->path, ',');
           $str = str_repeat('&nbsp;', $num*6).'||---';
           $data[$k]-> tname = $str.$v->tname; 
        }
    
        return view('admin.type.glance', ['data'=>$data,'request'=>$request->all(),'title' => '分类信息']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = DB::table('types')
        ->select('types.*',DB::raw("concat(types.path,',',types.tid) as sortpath"))
        ->orderBy('sortpath')
        ->get();
        foreach ($data as $k => $v)
        {
           $num = substr_count($v->path, ',');
           $str = str_repeat('&nbsp;', $num*6).'||---';
           $data[$k]-> tname = $str.$v->tname; 
        }
       
        return view('admin.type.add',['data'=>$data,'title'=>'添加分类']);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request -> except('_token');
        if ($data['pid'] != 0) 
        {
            $parent_path = DB::table('types')->where('tid',$data['pid'])->first()->path;
            $self_path= $parent_path.','.$data['pid'];
            $data['path'] = $self_path;
        }else
        {   
            $data['path'] = 0;
        }
        $res = DB::table('types')->insert($data);
        if ($res) 
        {
           return redirect('/admin/type')->with(['info'=>'添加成功']);
        }else
        {
            return back()->with(['info'=>'添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($tid)
    {
        $data = DB::table('types')
        ->select('types.*',DB::raw("concat(types.path,',',types.tid) as sortpath"))
        ->orderBy('sortpath')
        ->get();
        $cateDate = DB::table('types')->where('tid',$tid)->first();
        foreach ($data as $k => $v)
        {
           $num = substr_count($v->path, ',');
           $str = str_repeat('&nbsp;', $num*6).'||---';
           $data[$k]-> tname = $str.$v->tname; 
        }
       // dd($data);
        return view('admin.type.edit',['data'=>$data,'title'=>'编辑分类','cateDate'=>$cateDate]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request -> except('_token','_method');
        $parent_path = DB::table('types')->where('tid',$data['pid'])->first()->path;
        $self_path= $parent_path.','.$data['pid'];
        $data['path'] = $self_path;
        $res = DB::table('types')-> where('tid',$id) ->update($data);
        if ($res) 
        {
           return redirect('/admin/type')->with(['info'=>'更新成功成功']);
        }else
        {
            return back()->with(['info'=>'更新失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('types')->where('pid',$id)->first();
        if ($res)
        {
            return back()->with(['info'=>'有子分类，不可以删除']);
        }
        $res = DB::table('types')->where('tid',$id)->delete();
        if ($res) 
        {
            return redirect('/admin/type')->with(['info'=>'删除成功']);
        }else
        {
            return back()->with(['info'=>'删除失败']);
        }
    }
}
